#include "DynamicSequenceTable.h"

// 基本增删查改接口
    // 顺序表初始化
    void SeqList::SeqListInit(size_t capacity)
    {
        cout << "开始初始化！" << endl;
        m_arr = new SLDataType[capacity];
        m_size = 0;
        m_capacity = capacity;
        memset(m_arr, 0, sizeof(SLDataType) * capacity);
        cout << "初始化成功！" << endl;
    }
    
    // 检查空间，如果满了，进行增容
    void SeqList::CheckCapacity()
    {
        cout << "开始检查空间！" << endl;
        if(m_size == m_capacity)
        {
            SLDataType *tmp = new SLDataType[m_capacity*2];
            m_capacity *= 2;
            for(int i = 0; i < m_size; i++)
            {
                tmp[i] = m_arr[i];
            }
            delete[] m_arr; // 删除旧的m_arr
            m_arr = tmp; // 将m_arr指向新的内存
            cout << "扩容成功，当前容量为：" << m_capacity << endl;
        }
        else
        {
            cout << "空间未满，无需扩容！" << endl;
        }
    }

    // 顺序表尾插
    void SeqList::SeqListPushBack(SLDataType x)
    {
        CheckCapacity();
        m_arr[m_size++] = x;
        cout << "尾插成功！插入数据为：" << x << endl;
    }

    // 顺序表尾删
    void SeqList::SeqListPopBack()
    {
        m_size--;
        cout << "尾删成功！" << endl;
    }
    // 顺序表头插
    void SeqList::SeqListPushFront(SLDataType x)
    {
        CheckCapacity();
        for(int i = m_size; i > 0; i--)
        {
            m_arr[i] = m_arr[i-1];
        }
        m_arr[0] = x;
        m_size++;
        cout << "头插成功！插入数据为：" << x << endl;
    }
    // 顺序表头删
    void SeqList::SeqListPopFront()
    {
        for(int i = 0; i < m_size-1; i++)
        {
            m_arr[i] = m_arr[i+1];
        }
        m_size--;
        cout << "头删成功！" << endl;
    }

    // 顺序表查找
    void SeqList::SeqListFind(SLDataType x)
    {
        int cnt = 0;
        for(int i = 0; i < m_size; i++)
        {
            if(m_arr[i] == x)
            {
                cout << "找到了，" << x << "的下标为：" << i << endl;
                cnt++;
            }
        }
        if(cnt == 0)
        {
            cout << "没找到！" << endl;
        }
    }

    // 顺序表在pos位置插入x
    void SeqList::SeqListInsert(size_t pos, SLDataType x)
    {
        CheckCapacity();
        if(pos < 1 || pos > m_size+1)
        {
            cout << "插入位置不合法！" << endl;
            return;
        }
        
        for(int i = m_size; i > pos-1; i--)
        {
            m_arr[i] = m_arr[i-1];
        }
        m_arr[pos-1] = x;
        m_size++;
        cout << "插入成功！插入数据为：" << x << endl;
    }

    // 顺序表删除pos位置的值
    void SeqList::SeqListErase(size_t pos)
    {
        if(pos < 1 || pos > m_size)
        {
            cout << "删除位置不合法！" << endl;
            return;
        }
        for(int i = pos-1; i < m_size-1; i++)
        {
            m_arr[i] = m_arr[i+1];
        }
        m_size--;
        cout << "删除成功！" << endl;
    }

    // 顺序表销毁
    void SeqList::SeqListDestory()
    {
        delete[] m_arr;
        m_arr = nullptr;
        m_size = 0;
        m_capacity = 0;
        cout << "销毁成功！" << endl;
    }

    // 顺序表打印
    void SeqList::SeqListPrint()
    {
        if(m_size == 0)
        {
            cout << "顺序表为空！" << endl;
            return;
        }
        for(int i = 0; i < m_size; i++)
        {
            cout << m_arr[i] << " ";
        }
        cout << endl;
    }